package kr.ac.chungbuk;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class IBoardDAO {
	private static SqlMapClient sqlMapper;
	public IBoardDAO() {		
	}
	static {
		try {
			Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
			sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
			System.out.println("sqlMapper 연결 성공:" + sqlMapper);
			reader.close();
		} catch (IOException e) {
			// Fail fast.
			throw new RuntimeException(
					"Something bad happened while building the SqlMapClient instance."
							+ e, e);
		}
	}
	public void insertUser(UserDTO user){
		try {
			sqlMapper.insert("insertUser", user);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	public List selectAll(){
		try {
			List list = sqlMapper.queryForList("selectAll");
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}return null; 
		 //sqlMapper.queryForObject(arg0, arg1)
		 //sqlMapper.delete(arg0, arg1)
		 //sqlMapper.update( arg0, arg1)
	}
	
	public ItemDTO selectOne(int item_ID){
		ItemDTO item = null;
		
		try {
			item = (ItemDTO)sqlMapper.queryForObject("selectOne",item_ID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return item;
	}
	public UserDTO selectTwo(String user_id){
		UserDTO user = null;
		
		try {
			user = (UserDTO)sqlMapper.queryForObject("selectTwo",user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	/*public BoardDTO selectOne(String title){
		BoardDTO board=null;
		try {
			board = (BoardDTO)sqlMapper.queryForObject("selectOne",title);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return board;
	}*/
	public void delete(String title){
		try {
			int n=sqlMapper.delete("delete",title);
			System.out.println("delete n="+n);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/*public void  update(BoardDTO board){
		try {
			int n=sqlMapper.update("update",board);
			System.out.println("update n="+n);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	/* create table  tt (no integer auto_increment primary key, title varchar(100)); */ 
	public void insertSale(SaleDTO sale){
		try {
			sqlMapper.insert("insertSale",sale);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Integer findMaxSaleId(){
		int max=0;
		try {
			max=(Integer)sqlMapper.queryForObject("selectMaxSaleId");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return max;
	}
	public void insertSaleLine(SaleLineDTO saleline){
		try {
			sqlMapper.insert("insertSaleLine",saleline);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
